#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer.setInterval(ui->timeInterval->value());
    bool flag = connect(&timer, SIGNAL(timeout()),
                        this, SLOT(on_timeout()));
    if(flag){
        qDebug()<<"successed";
    }
    else {
        qDebug()<<"failed";
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_timeout(){
    QTime curTime = QTime::currentTime();
    ui->hour->display(curTime.hour());
    ui->minute->display(curTime.minute());
    ui->second->display(curTime.second());
}

void MainWindow::on_btnStart_clicked()
{
    timer.start();
    counter.start();
    ui->btnStart->setEnabled(false);
    ui->btnStop->setEnabled(true);
    ui->timeInterval->setEnabled(false);
}

void MainWindow::on_btnStop_clicked()
{
    timer.stop();
    int msec = counter.elapsed();
    int ms = msec%1000;
    int sec = msec/1000;
    QString text = QString::asprintf("%d秒,%d毫秒", sec, ms);
    ui->editTime->setText(text);
    ui->btnStop->setEnabled(false);
    ui->btnStart->setEnabled(true);
    ui->timeInterval->setEnabled(true);
}

void MainWindow::on_sbInter_valueChanged(const QString &arg1)
{
    timer.setInterval(arg1.section(' ',0,0).toInt());
}
